//
//  RetweetedView.swift
//  SinaBlog
//
//  Created by macs on 15/11/15.
//  Copyright © 2015年 BlackTail. All rights reserved.
//

import UIKit
import SnapKit
class RetweetedView: UIView {
    
    var selfConstraint : Constraint?
    var retweeteds : Status? {
        
        didSet {
            
            titleLable.attributedText = EmojiManage.shareManage.getAttributeText(retweeteds?.text ?? "", font: titleLable.font)
            
            //取消约束
            self.selfConstraint?.uninstall()
            
            if let purls = retweeteds?.pictureUrl where purls.count > 0  {
                pictureView.imageUrls = purls
                //有 pitureview 重置约束
                snp_makeConstraints(closure: { (make) -> Void in
                    self.selfConstraint = make.bottom.equalTo(pictureView.snp_bottom).constraint
                })
                
                pictureView.hidden = false
                
            } else {
                
                //没有 pictureView 时约束
                snp_makeConstraints(closure: { (make) -> Void in
                    self.selfConstraint = make.bottom.equalTo(titleLable.snp_bottom).constraint
                })
                
                pictureView.hidden = true
                
            }
        }
    }
    
    //3.重写构造方法
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //2.添加子控件,设置约束
    func setupUI() {
        
        addSubview(titleLable)
        addSubview(pictureView)
        //添加文字内容
        titleLable.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(snp_top)
            make.left.equalTo(snp_left).offset(statuscellMargin)
        }
        //添加图片显示
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(titleLable.snp_bottom).offset(statuscellMargin)
            make.left.equalTo(titleLable.snp_left)
        }
        //为自身添加约束
        snp_makeConstraints { (make) -> Void in
            self.selfConstraint =  make.bottom.equalTo(pictureView.snp_bottom).constraint
        }
        
    }
    //1.懒加载控件
    lazy var titleLable : UILabel = UILabel(title: "转发微博---", size: 14, color: UIColor.darkGrayColor(), margin: statuscellMargin)
    lazy var pictureView : PictureView = PictureView()
}
